//
//  SFAccountsViewController.m
//  Sofia
//
//  Created by Anton Chebotaev on 24/11/08.
//  Copyright 2008 IFMO. All rights reserved.
//

#import "SFAccountsViewController.h"
#import "SFUserAccount.h"


@implementation SFAccountsViewController
@synthesize defaultAccountNumber;
- (void)willBeDisplayed
{
	// loading User Accounts
	if(!userAccounts)
		userAccounts = [[NSMutableArray alloc] init];
	[self setValue:[NSMutableArray arrayWithArray:[self loadSFUserAccounts]]
			forKey:@"userAccounts"];
	
	// loading Default Account Number
	defaultAccountNumber = [self loadDefaultAccountNumber];
}

- (void)dealloc
{
	[userAccounts release];
	[super dealloc];
}

- (NSString *)title
{
	return NSLocalizedString(@"Accounts", @"Title of 'General' preference pane");
}

- (NSString *)identifier
{
	return @"SFAccountsPane";
}

- (NSImage *)image
{
	return [NSImage imageNamed:@"NSAccounts"];
}

#pragma mark Accounts Dialog
- (IBAction)showAccountDialog:(id)sender
{
	[NSApp beginSheet: accountDialog
	   modalForWindow: [[self view] window]
		modalDelegate: self
	   didEndSelector: nil
		  contextInfo: nil];	
}
- (IBAction)hideAccountDialog:(id)sender
{
	[NSApp endSheet: accountDialog];
	[accountDialog orderOut: self];	
}

#pragma mark Default User Accounts
- (IBAction)addSFUserAccount:(id)sender
{
	NSString *unm = [loginField stringValue];
	if([unm isEqual:@""])
	{
		NSRunAlertPanel(@"Error", @"Can not add account with empty username", @"OK", @"", @"");
		return;
	}
	
	NSString *pwd = [passwordField stringValue];
	NSLog(@"Adding username (%@) with password (%@)", unm, pwd);
	
    LJAccount *newAccount = [[LJAccount alloc] initWithUsername:unm];
	NSLog([newAccount identifier]);
	
	//SFUserAccount *tempUser = [[SFUserAccount alloc] initWithName:unm password:pwd];
	[userAccountsController addObject:newAccount];
    [newAccount release];

	
	// We assume that accountDialog panel is openet, so we are tyring to close it now
	// Also clearing content in text fields
	[loginField setStringValue:@""];
	[passwordField setStringValue:@""];
	[self hideAccountDialog:nil];

	
	// Saving everything to defaults
	[self saveDefaultAccountNumber:nil];
	[self saveSFUserAccounts:nil];
}

- (IBAction)removeSFUserAccount:(id)sender
{
	[userAccountsController remove:nil];
	// We just saved userAccounts array, so we have to write it to defaults
	[self saveSFUserAccounts:nil];
}

- (NSMutableArray *)loadSFUserAccounts
{
    NSData *userAccountsAsData = [[NSUserDefaults standardUserDefaults] objectForKey:SFAAccouns];
    return [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:userAccountsAsData]];	
}

- (IBAction)saveSFUserAccounts:(id)sender
{
    NSData *userAccountsAsData = [NSKeyedArchiver archivedDataWithRootObject:userAccounts];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setObject:userAccountsAsData forKey:SFAAccouns];
}

#pragma mark Default Account Number
- (IBAction)saveDefaultAccountNumber:(id)sender
{
	defaultAccountNumber = [sender selectedRow];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:defaultAccountNumber forKey:SFADefaultAccountNumber];
	[sender setNeedsDisplay:YES];
}

- (int)loadDefaultAccountNumber
{
    return [[NSUserDefaults standardUserDefaults] integerForKey:SFADefaultAccountNumber];	
}

#pragma mark Delegates
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
	if([[userAccountsController arrangedObjects] objectAtIndex:rowIndex] == [userAccounts objectAtIndex:defaultAccountNumber])
	{
		[defaultButton setState:NSOnState];
	}
}

@end
